import CryptoJS from 'crypto-js'
import {message} from 'ant-design-vue'
// 鉴权 返回真正的url接口
var key={APPID:'a1ef9519',API_SECRET:'MGE3NDExZWUyMDg4NTAxNGVkYTIzMDg0',API_KEY:'339728badfa245eac0e62094b3ccf641'}
export default function getWebsocketUrl(tkey=key) {
    return new Promise((resolve, reject) => {
        var apiKey = tkey.API_KEY
        var apiSecret = tkey.API_SECRET
        var url = 'wss://spark-api.xf-yun.com/v2.1/chat'
        var host = location.host
        var date = new Date().toGMTString()
        var algorithm = 'hmac-sha256'
        var headers = 'host date request-line'
        var signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2.1/chat HTTP/1.1`
        var signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret)
        var signature = CryptoJS.enc.Base64.stringify(signatureSha)
        var authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`
        var authorization = btoa(authorizationOrigin)
        url = `${url}?authorization=${authorization}&date=${date}&host=${host}`
        resolve(url)
    })
}
export function changekey(tkey){
    console.log(key);
    key=tkey
    if (tkey.APPID==''||tkey.API_SECRET==''||tkey.API_KEY=='') {
        message.error('请填写完整信息')
        key={APPID:'a1ef9519',API_SECRET:'MGE3NDExZWUyMDg4NTAxNGVkYTIzMDg0',API_KEY:'339728badfa245eac0e62094b3ccf641'}
    }
}
